//
//  WZMenuView.m
//  ETong
//
//  Created by wanzhao on 15/12/4.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZMenuView.h"
#import "WZMenuCell.h"

@interface WZMenuView ()<UITableViewDataSource, UITableViewDelegate>


@end

@implementation WZMenuView


- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.dataSource = self;
        self.delegate = self;

    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.menuDataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return OppositeHeightRate(50);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WZMenuCell *cell = [WZMenuCell menuCellWithTableView:tableView];
    WZProcedure *pro = self.menuDataArray[indexPath.row];
    cell.procedure = pro;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     WZProcedure *pro = self.menuDataArray[indexPath.row];
    pro.isSelected = YES;
    [self.menuDataArray enumerateObjectsUsingBlock:^(WZProcedure *obj, NSUInteger idx, BOOL *stop) {
        if (![pro.name isEqualToString:obj.name]) {
            obj.isSelected = NO;
        }
    }];
    
    if ([self.menueViewDelegate respondsToSelector:@selector(menueViewDidSelectWithName:total:)]) {
        [self.menueViewDelegate menueViewDidSelectWithName:pro.name total:pro.total];
    }
    
    [self reloadData];
}

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition {
    
    WZProcedure *pro = self.menuDataArray[indexPath.row];
    pro.isSelected = YES;
    [self.menuDataArray enumerateObjectsUsingBlock:^(WZProcedure *obj, NSUInteger idx, BOOL *stop) {
        if (![pro.name isEqualToString:obj.name]) {
            obj.isSelected = NO;
        }
    }];
    
    if ([self.menueViewDelegate respondsToSelector:@selector(menueViewDidSelectWithName:total:)]) {
        [self.menueViewDelegate menueViewDidSelectWithName:pro.name total:pro.total];
    }
    
    [self reloadData];
}

- (void)setMenuDataArray:(NSArray *)menuDataArray
{
    _menuDataArray = menuDataArray;
    [self reloadData];
}
@end
